package com.honeybees.framework.datatables.ajax;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * <dl>
 * <dt><b> datatables 返回数据结构 </b></dt>
 * <p>
 * <dd><b>https://www.datatables.net/manual/server-side Returned data</b></dd>
 * </dl>
 * <p>
 * Copyright (c) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @since 2017-04-07 20:02
 */
public class DatatablesResponse<T> implements Serializable {

    private static final long serialVersionUID = -674720026175101099L;

    /**
     * 浏览器cache的编号，递增不可重复
     */
    private int draw;

    /**
     * 数据总条数
     */
    private int recordsTotal;
    /**
     * 查询到的数据条数：data.size()？
     */
    private int recordsFiltered;
    /**
     * 页面数据
     */
    private List<T> data;

    /**
     * 错误信息
     */
    private String error;

    private DatatablesResponse() {
        this.data = Collections.emptyList();
        this.recordsTotal = 0;
        this.recordsFiltered = 0;
        this.draw = 0;
    }

    private DatatablesResponse(List<T> dataList, int totalCount, int draw) {
        if (dataList == null) {
            this.data = Collections.emptyList();
            this.recordsTotal = 0;
            this.recordsFiltered = 0;
            this.draw = 0;
        } else {
            this.data = dataList;
            this.recordsTotal = totalCount;
            this.recordsFiltered = totalCount; // FIXME 怎么过滤？
            this.draw = draw;
        }
    }

    /**
     * 构建返回数据（空）
     *
     * @param <T> 返回数据类型
     * @return 返回数据
     */
    public static <T> DatatablesResponse<T> getInstance() {
        return new DatatablesResponse<T>();
    }

    /**
     * 构建返回数据
     *
     * @param criterias  DatatablesRequest
     * @param dataList   数据
     * @param totalCount 数据总条数
     * @param <T>        返回数据类型
     * @return 返回数据
     */
    public static <T> DatatablesResponse<T> getInstance(DatatablesRequest criterias, List<T> dataList, int totalCount) {
        int draw = 0;
        if (criterias != null) {
            draw = criterias.getDraw();
        }
        return new DatatablesResponse<T>(dataList, totalCount, draw);
    }

    /**
     * 构建返回数据
     *
     * @param dataList   数据
     * @param totalCount 数据总条数
     * @param <T>        返回数据类型
     * @return 返回数据
     * @version 2017-04-10 10:47
     */
    public static <T> DatatablesResponse<T> getInstance(List<T> dataList, int totalCount) {
        int draw = 0;
        return new DatatablesResponse<T>(dataList, totalCount, draw);
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(int recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public int getRecordsFiltered() {
        return recordsFiltered;
    }

    public void setRecordsFiltered(int recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}
